#!/bin/sh
# installation / upgrade script smartcard 2.0
# updates to sc20 v1.3 - 2008-12-01 henrik andreasson


DISTFILES_INSTALL=/usr/local/install
# not supported to change this yet
INSTALL_INTO=/usr/local

EJBCA_DISTNAME=ejbca_Rel_3_5_12
HTMF_DISTNAME=hardtokenmgmt_1.2.0-beta1
JBOSS_DISTNAME=jboss-4.2.3.GA-jdk6
JBOSS_DIRNAME=jboss-4.2.3.GA
ANT_DISTNAME=apache-ant-1.7.1
MYSQL_DISTNAME=mysql-connector-java-5.1.7
CUSTOMPROJECT_DISTNAME=htmf_custom_project
MYSQL_JARNAME=mysql-connector-java-5.1.7-bin.jar
PCSCD_DISTNAME=pcsc-1.4.102-binary-x86_64-sles10sp2
PCSCD_DIRNAME=pcsc-1.4.102
PCSCD_INITD=pcscd-usr-local-init.d
JARSIGNER=jarsigner.jks
CUSTOM_SETTINGS=custom-settings-script.sh
SYSCHECK=syscheck-1.5.4



JAVA_DISTNAME=jdk-6u16-linux-x64
OMNIKEYDRIVER_DISTNAME=ifdokccid_lnx_x64-3.4.0
platform=`uname -p` 
if [ "x${platform}" == "xx86_64" ] ; then
	echo "you're running on a 64 bit platform, installing 64 bit java"
	# 64 bits os
	JAVA_DISTNAME=jdk-6u16-linux-x64

	# 64:bit version of the driver
	OMNIKEYDRIVER_DISTNAME=ifdokccid_lnx_x64-3.4.0

	# 64:bit version of PRIMECARD_DISTNAME
	PRIMECARD_DISTNAME=PrimeCard-tags_Rel_1_8_5-java1.6-ejbca3.5-64bit
else
	echo "you're running on a 32 bit platform, installing 32 bit java"
	# 32 bits os
	JAVA_DISTNAME=jdk-6u16-linux-i586

	# 32 bit version of the driver
	OMNIKEYDRIVER_DISTNAME=ifdokccid_lnx-3.4.0

	# 32:bit version of PRIMECARD_DISTNAME
	PRIMECARD_DISTNAME=PrimeCard-tags_Rel_1_8_5-java1.6-ejbca3.5
fi

PRIMECARD_DIRNAME=PrimeCard-tags_Rel_1_8_5-java1.6-ejbca3.5
PRIMECARD_JARDIST=dist-1.6-3.5
JAVA_DIRNAME=jdk1.6.0_11
JCE_DISTNAME=jce_policy-6

JBOSS_INITD=jboss-init.d

runningUserId=`id --user`
if [ ${runningUserId} -ne 0 ] ; then
	echo "$0 must be run as root"
	exit
fi

if [ ! -d ${DISTFILES_INSTALL} ] ; then
	mkdir -p ${DISTFILES_INSTALL}
fi

##### start copy zip:s to ${DISTFILES_INSTALL} #########
echo "copy all packages to ${DISTFILES_INSTALL}"
if [ ! -f ${DISTFILES_INSTALL}/${EJBCA_DISTNAME}.zip ] ; then 	
	echo "copying ${EJBCA_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${EJBCA_DISTNAME}.zip     ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${HTMF_DISTNAME}.zip ] ; then
	echo "copying ${HTMF_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${HTMF_DISTNAME}.zip      ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${JBOSS_DISTNAME}.zip ] ; then
	echo "copying ${JBOSS_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${JBOSS_DISTNAME}.zip     ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${ANT_DISTNAME}-bin.zip ] ; then
	echo "copying ${ANT_DISTNAME}-bin.zip to ${DISTFILES_INSTALL}"
	cp ${ANT_DISTNAME}-bin.zip   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${PCSCD_DISTNAME}.zip ] ; then
	echo "copying ${PCSCD_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${PCSCD_DISTNAME}.zip ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${PRIMECARD_DISTNAME}.zip ] ; then
	echo "copying ${PRIMECARD_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${PRIMECARD_DISTNAME}.zip ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${JAVA_DISTNAME}.bin ] ; then
	echo "copying ${JAVA_DISTNAME}.bin to ${DISTFILES_INSTALL}"
	cp ${JAVA_DISTNAME}.bin      ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${JCE_DISTNAME}.zip ] ; then
	echo "copying ${JCE_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${JCE_DISTNAME}.zip       ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${OMNIKEYDRIVER_DISTNAME}.tar.gz ] ; then
	echo "copying ${OMNIKEYDRIVER_DISTNAME}.tar.gz to ${DISTFILES_INSTALL}"
	cp ${OMNIKEYDRIVER_DISTNAME}.tar.gz       ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${JBOSS_INITD} ] ; then
	echo "copying ${JBOSS_INITD} to ${DISTFILES_INSTALL}"
	cp ${JBOSS_INITD}   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${PCSCD_INITD} ] ; then
	echo "copying ${PCSCD_INITD} to ${DISTFILES_INSTALL}"
	cp ${PCSCD_INITD}   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${MYSQL_DISTNAME}.tar.gz ] ; then
	echo "copying ${MYSQL_DISTNAME}.tar.gz to ${DISTFILES_INSTALL}"
	cp ${MYSQL_DISTNAME}.tar.gz   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${CUSTOMPROJECT_DISTNAME}.zip ] ; then
	echo "copying ${CUSTOMPROJECT_DISTNAME}.zip to ${DISTFILES_INSTALL}"
	cp ${CUSTOMPROJECT_DISTNAME}.zip   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${JARSIGNER} ] ; then
	echo "copying ${JARSIGNER} to ${DISTFILES_INSTALL}"
	cp ${JARSIGNER}   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${CUSTOM_SETTINGS} ] ; then
	echo "copying ${CUSTOM_SETTINGS} to ${DISTFILES_INSTALL}"
	cp ${CUSTOM_SETTINGS}   ${DISTFILES_INSTALL}
fi
if [ ! -f ${DISTFILES_INSTALL}/${SYSCHECK}.zip ] ; then
	echo "copying ${SYSCHECK}.zip to ${DISTFILES_INSTALL}"
	cp ${SYSCHECK}.zip   ${DISTFILES_INSTALL}
fi


##### end copy zip:s to ${DISTFILES_INSTALL} #########

##### start unzip-ing #########
# unzip in ${INSTALL_INTO}
cd ${INSTALL_INTO}

if [ -x ${EJBCA_DISTNAME} ] ;then
	echo "${EJBCA_DISTNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${EJBCA_DISTNAME}.zip 
fi

if [ -x ${HTMF_DISTNAME} ] ;then
	echo "${HTMF_DISTNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${HTMF_DISTNAME}.zip
fi

if [ -x ${JBOSS_DIRNAME} ] ;then
	echo "${JBOSS_DIRNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${JBOSS_DISTNAME}.zip
fi

if [ -x ${ANT_DISTNAME} ] ;then
	echo "${ANT_DISTNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${ANT_DISTNAME}-bin.zip
fi

if [ -x ${MYSQL_DISTNAME} ] ;then
	echo "${MYSQL_DISTNAME} exists, skipping"
else
	tar zxvf ${DISTFILES_INSTALL}/${MYSQL_DISTNAME}.tar.gz
fi

if [ -x ${CUSTOMPROJECT_DISTNAME} ] ;then
	echo "${CUSTOMPROJECT_DISTNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${CUSTOMPROJECT_DISTNAME}.zip
fi

if [ -x ${PCSCD_DIRNAME} ] ;then
	echo "${PCSCD_DIRNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${PCSCD_DISTNAME}.zip
fi

if [ -x ${PRIMECARD_DIRNAME} ] ;then
	echo "${PRIMECARD_DIRNAME} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${PRIMECARD_DISTNAME}.zip
fi

if [ -x ${OMNIKEYDRIVER_DISTNAME} ] ;then
	echo "${OMNIKEYDRIVER_DISTNAME} exists, skipping"
else
	tar zxvf ${DISTFILES_INSTALL}/${OMNIKEYDRIVER_DISTNAME}.tar.gz
fi

if [ -d ${INSTALL_INTO}/${HTMF_DISTNAME}/ ] ;then
	cp ${DISTFILES_INSTALL}/${JARSIGNER} ${INSTALL_INTO}/${HTMF_DISTNAME}/${JARSIGNER}
else
	echo "${INSTALL_INTO}/${HTMF_DISTNAME}/ dont exist, this is BIG TROULBES skipping"
	exit
fi

if [ -d "${INSTALL_INTO}/${HTMF_DISTNAME}" ] ;then
	cp ${DISTFILES_INSTALL}/${CUSTOM_SETTINGS} ${INSTALL_INTO}/${HTMF_DISTNAME}/ 
	chmod 755 ${INSTALL_INTO}/${HTMF_DISTNAME}/${CUSTOM_SETTINGS}
else
	echo "${INSTALL_INTO}/${HTMF_DISTNAME}/ dont exist, this is BIG TROULBES skipping"
	exit
fi


if [ -x ${JAVA_DIRNAME} ] ;then
	echo "${JAVA_DIRNAME} exists, skipping"
else
	sh ${DISTFILES_INSTALL}/${JAVA_DISTNAME}.bin
	unzip ${DISTFILES_INSTALL}/${JCE_DISTNAME}.zip
	mv ${INSTALL_INTO}/jce/* ${JAVA_DIRNAME}/jre/lib/security/
	rm -rf ${INSTALL_INTO}/jce
fi

if [ -x ${SYSCHECK} ] ;then
	echo "${SYSCHECK} exists, skipping"
else
	unzip ${DISTFILES_INSTALL}/${SYSCHECK}.zip
fi

##### end unzip-ing #########

##### start create user #####

grep -q  jboss /etc/passwd
if [ $? -ne 0 ] ; then
	# create a user
	groupadd jboss
	useradd -m -g jboss jboss
fi

##### end create user #####

##### start make softlinks to the active versions #####

mv ${INSTALL_INTO}/${JBOSS_DIRNAME} ${INSTALL_INTO}/jboss

rm -f htmf
ln -s ${HTMF_DISTNAME} ${INSTALL_INTO}/htmf

rm -f ejbca
ln -s ${EJBCA_DISTNAME} ${INSTALL_INTO}/ejbca

rm -f java
ln -s ${JAVA_DIRNAME} ${INSTALL_INTO}/java

rm -f ant
ln -s ${ANT_DISTNAME} ${INSTALL_INTO}/ant

rm -f primecard
ln -s ${PRIMECARD_DIRNAME} ${INSTALL_INTO}/primecard

rm -f pcsc 
ln -s ${PCSCD_DIRNAME} ${INSTALL_INTO}/pcsc

rm -f htmf-setup
ln -s ${INSTALL_INTO}/htmf/src/inst/smartcard20/htmf-setup ${INSTALL_INTO}
chmod 755 ${INSTALL_INTO}/htmf/src/inst/smartcard20/htmf-setup

# copy jboss-x.y.z.zip for the htmf-setup re-unzip:s it
cp ${DISTFILES_INSTALL}/${JBOSS_DISTNAME}.zip ${INSTALL_INTO}

# link the right env file
rm -f environment
ln -s ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment ${INSTALL_INTO}/environment 

rm -f syscheck
ln -s ${SYSCHECK} ${INSTALL_INTO}/syscheck


##### start make softlinks to the active versions #####

##### start set options #####

# add ant opts for more mem
grep -q 'ANT_OPTS' ${INSTALL_INTO}/environment
if [ $? -ne 0 ] ; then
	echo 'export ANT_OPTS="-Xms512m -Xmx512m"' >> ${INSTALL_INTO}/environment
fi

# mysql driver home
grep -q 'MYSQLDRIVER' ${INSTALL_INTO}/environment
if [ $? -ne 0 ] ; then
	echo "export MYSQLDRIVER=${INSTALL_INTO}/${MYSQL_DISTNAME}/${MYSQL_JARNAME}" >> ${INSTALL_INTO}/environment
fi

# set jboss path
perl -pi -e "s#JBOSS_HOME.*#JBOSS_HOME=${INSTALL_INTO}/jboss#" ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment

# add environment sourcing to jboss users shell
jbossenvhome=`grep "${INSTALL_INTO}/environment" /home/jboss/.bashrc`
if [ "x${jbossenvhome}" = "x" ] ; then
	echo "# htmf env" >> /home/jboss/.bashrc
	echo "if [ -f ${INSTALL_INTO}/environment ]; then " >> /home/jboss/.bashrc
	echo "    . ${INSTALL_INTO}/environment " >> /home/jboss/.bashrc
	echo "fi" >> /home/jboss/.bashrc
fi

##### end set options #####

##### start init.d scripts #####

### pcscd init.d
cp ${DISTFILES_INSTALL}/${PCSCD_INITD} /etc/init.d/pcscd-usr-local
chmod 755 /etc/init.d/pcscd-usr-local
if [ -f /etc/init.d/pcscd ] ;then
	mv /etc/init.d/pcscd /etc/init.d/pcscd-system-disabled-by-sc20
	chmod 644 /etc/init.d/pcscd-system-disabled-by-sc20
fi

if [ -d /etc/init.d/rc3.d/ ] ;then 
	rm -f /etc/init.d/rc3.d/S91pcscd-usr-local /etc/init.d/rc0.d/K03pcscd-usr-local /etc/init.d/rc6.d/K03pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc3.d/S91pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc0.d/K03pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/init.d/rc6.d/K03pcscd-usr-local
elif [ -d /etc/rc3.d/ ] ; then
	rm -f /etc/rc3.d/S91pcscd-usr-local /etc/rc0.d/K02pcscd-usr-local /etc/rc6.d/K03pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/rc3.d/S91pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/rc0.d/K02pcscd-usr-local
	ln -s /etc/init.d/pcscd-usr-local /etc/rc6.d/K02pcscd-usr-local
else
	echo "cant find rc3.d and friends to make links to /etc/init.d/jboss no autostart/shutdown will be made"
fi

#### jboss init.d 
cp ${DISTFILES_INSTALL}/${JBOSS_INITD} /etc/init.d/jboss
chmod 755 /etc/init.d/jboss
if [ -d /etc/init.d/rc3.d/ ] ;then 
	rm -f /etc/init.d/rc3.d/S92jboss /etc/init.d/rc0.d/K02jboss /etc/init.d/rc6.d/K02jboss
	ln -s /etc/init.d/jboss /etc/init.d/rc3.d/S92jboss
	ln -s /etc/init.d/jboss /etc/init.d/rc0.d/K02jboss
	ln -s /etc/init.d/jboss /etc/init.d/rc6.d/K02jboss
elif [ -d /etc/rc3.d/ ] ; then
	rm -f /etc/rc3.d/S92jboss /etc/rc0.d/K02jboss /etc/rc6.d/K02jboss
	ln -s /etc/init.d/jboss /etc/rc3.d/S92jboss
	ln -s /etc/init.d/jboss /etc/rc0.d/K02jboss
	ln -s /etc/init.d/jboss /etc/rc6.d/K02jboss
	
else
	echo "cant find rc3.d and friends to make links to /etc/init.d/jboss no autostart/shutdown will be made"
fi

##### end init.d scripts #####

#### start misc stuff ####

### primecard env settings
chmod 755 ${PRIMECARD_DIRNAME}/bin/*.sh
rm -rf ${PRIMECARD_DIRNAME}/bin/*.bat
grep -q 'PRIMECARD_HOME' ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
if [ $? -ne 0 ] ; then
        echo "export PRIMECARD_HOME=${INSTALL_INTO}/primecard" >> ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
        echo "export LD_LIBRARY_PATH=${INSTALL_INTO}/primecard/dist-1.6-3.5/:${INSTALL_INTO}/pcsc/lib" >> ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
fi
perl -pi -e "s#PRIMECARD_HOME.*#PRIMECARD_HOME=${INSTALL_INTO}/primecard#" ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
perl -pi -e "s#LD_LIBRARY_PATH=.*#LD_LIBRARY_PATH=${INSTALL_INTO}/primecard/dist-1.6-3.5/:${INSTALL_INTO}/pcsc/lib#" ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment


### increse JBOSS Perm Space
grep -q 'JAVA_OPTS' ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
if [ $? -ne 0 ] ; then
        echo "export JAVA_OPTS=\"-Xms512m -Xmx512m -XXMaxPermSize=256m\"" >> ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
else
	perl -pi -e "s/^JAVA_OPTS=(.*)$/JAVA_OPTS=$1 -XXMaxPermSize=256m/gio" ${INSTALL_INTO}/htmf/src/inst/smartcard20/environment
fi

# change owner of the files
chown -R  jboss.jboss ${INSTALL_INTO}/${PRIMECARD_DIRNAME}
chown -R  jboss.jboss ${INSTALL_INTO}/${HTMF_DISTNAME}
chown -R  jboss.jboss ${INSTALL_INTO}/${EJBCA_DISTNAME}
chown -R  jboss.jboss ${INSTALL_INTO}/jboss


if [ -f /usr/sbin/pcscd -o -f /usr/local/pcsc/sbin/pcscd ] ; then 
	echo "pcscd is installed, will add support for primecardHSM (to store the CA:s private keys on a smartcard)"
	cd ${INSTALL_INTO}/${OMNIKEYDRIVER_DISTNAME}
	if [ -d /usr/local/pcsc/pcsc/drivers ] ; then
		./install -d /usr/local/pcsc/pcsc/drivers
	elif [ -d /usr/lib64/readers ] ; then
		./install -d /usr/lib64/readers
	elif [ -d /usr/lib/pcsc/drivers/ ] ; then
		./install -d /usr/lib/pcsc/drivers/
	fi
	/etc/init.d/pcscd-usr-local restart	
else
	echo "pcscd is NOT installed will not add suport for primecardHSM (to store the CA:s private keys on a smartcard)"
	echo "in debian: apt-get install pcscd"
	echo "in suse: add pcscd from custom build (we use 1.4.102)"
	echo "then rerun this script $0"
fi

#### end misc stuff ####

echo "install of files done, to run a setup do:"
echo "cd ${INSTALL_INTO}; sudo ./htmf-setup"

